<?php

namespace app\modules\v1\controllers;

use Yii;
use yii\rest\ActiveController;
use yii\web\NotFoundHttpException;
use app\modules\v1\models\Log;
use app\modules\v1\models\Usermoney;

/**
 * json通用遵守
 * {
 * "id":"",         (记录ID)
 * "oid":"",        (订单ID)
 * "telephone":"",  (手机号码)
 * "status":"",     (金额状态,充值/消费)
 * "money":"",      (金额数目)
 * "createdate":"", (创建日期)
 * }
  {
  "id":"",         
  "oid":"",         
  "telephone":"13129711121",  
  "status":"sub",     
  "money":"50",      
  "createdate":"",
  "token":"MjAxNy0wMy0wNQ==147e47817cd4daa2cd8668cd3b2a97f9"
  }
 */
class UserMoneyRecordController extends ActiveController
{
    public $modelClass = 'app\modules\v1\models\Usermoneyrecord';
    
    public function actions(){
        
        $actions = parent::actions();
        
        unset($actions['index'],$actions['create'],$actions['delete'],$actions['view'],$actions['update']);
        
    }
    
    public function actionIndex($telephone){
        
        $this->log('GET',json_encode(Yii::$app->request->get(),JSON_UNESCAPED_UNICODE));
        
        $modelClass = $this->modelClass;
        
        $model = $modelClass::find()->where(['telephone' => $telephone])->all();
        
        return $model;
        
    }
    
    public function actionCreate(){
        
        $this->log('POST', Yii::$app->request->getRawBody());
        
        $model = new $this->modelClass;
        
        $data = json_decode(Yii::$app->request->getRawBody(),true);
        
        $this->tokenValidate($data['token']);
        
        $UserMoney = Usermoney::find()->where(['telephone' => $data['telephone']])->one();
        
        if(is_null($UserMoney)){
            $response = Yii::$app->response;
            $response->format = \yii\web\Response::FORMAT_JSON;
            return $response->data = ['telephone' => '404'];
        }
        
        $model->attributes = $data;
        
        $model->createdate = date('Y-m-d H:i:s');
        
        if($data['status'] == 'add'){
            $UserMoney->money = $UserMoney->money + $data['money'];
        }else if($data['status'] == 'sub'){
            $UserMoney->money = $UserMoney->money - $data['money'];
        }else{
            $response = Yii::$app->response;
            $response->format = \yii\web\Response::FORMAT_JSON;
            return $response->data = ['status' => '403'];
        }
        
        if(!$UserMoney->save()){
            return $UserMoney->getFirstErrors();
        }
        
        if(!$model->save()){
            return $model->getFirstErrors();
        }
        
        return $model;
    }
    
    public function actionUpdate($id){
/*        
        $this->log('PUT', Yii::$app->request->getRawBody());

        $model = $this->findModel($id);
        
        $data = json_decode(Yii::$app->request->getRawBody(),true);
        
        $model->attributes = $data;
        
        $this->tokenValidate($data['token']);
        
        if(!$model->save()){
            return $model->getFirstErrors();
        }
        
        return $model;
  */      
    }
    
    public function actionDelete($id){
    /*    
        $this->log('DELETE', json_encode(['ID' => $id]));
        
        $model = $this->findModel($id);
        
        $model->delete();
        */
    }
    
    public function actionView($id){
        
        $this->log('GET', json_encode(['ID' => $id]));
        
        $model = $this->findModel($id);

        return $model;
        
    }
    
    protected function findModel($id){
        
        $modelClass = $this->modelClass;
        
        $model = $modelClass::findOne($id);
        
        if($model != null){
            return $model;
        }else{
            throw new NotFoundHttpException('404');
        }
        
    }
    
    protected function log($op,$data){
        
        $log = new Log();
        
        $log->attributes = ['operation' => $op,'class' => 'UserMoneyRecord','requestData' => $data,'dateTime' => date('Y-m-d H:i:s'),'ip' => Yii::$app->request->userIP];
        
        if(!$log->save()){
            return $log->getFirstError();
        }
        
    }

    public function tokenValidate($token){
        if($token != Yii::$app->params['token']){
            throw new NotFoundHttpException;
        }else{
            return true;
        }
    }
    
}
